package com.chance.cc.crawler.prod.command.job.domain.news.meipai.trace;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * created by CC on 2020/11/26
 * mail 279020185@qq.com
 */
public class MeiPaiHotCommentCrawlerScheduleJob extends NewsCommonScript {//-
    public static String domain = "meipai";//站点-
    public static String site = "carSeries";//站点-

    public static void main(String[] args) {
        publishCommentCrawlerScheduleJobInfo();//-
    }

    /**
     * 发布任务信息
     *
     * @return
     */
    public static CrawlerJob publishCommentCrawlerScheduleJobInfo() {//-
        CrawlerJob crawlerJob = commentCrawlerScheduler();//采集-

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();//定时采集作业
        crawlerScheduleJob.setDomain(domain);//设置域
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));//设置采集信息
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());//工作标签
        crawlerScheduleJob.setNote("美拍热门回溯5点定时采集");//采集提示-
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());//生成爬虫key
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);//发布标签命令添加或修改脚本调度工作
        System.out.println("发布作业：" + page.getRawText());//打印文本
        return crawlerJob;//返回工作爬虫
    }

    public static CrawlerJob commentCrawlerScheduler() {
        //文章采集
        String url = "https://www.meipai.com/medias/hot";//-
        String site_biz = "hot";//实时-
        HttpConfig httpConfig = HttpConfig.me(domain);//配置domain
        httpConfig.setHttpSite(site);//配置site

        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpConfig(httpConfig)//-
                .domain(domain)
                .httpUrl(url)
                .recordKey(url)
                .releaseTime(System.currentTimeMillis())
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
//                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-", "filter", domain, site, site_biz, "queue")))//根据redis中的值去重（不是本地）-
//                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24,null))
                .proxy(proxy)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
//                .needParsed(true)
//                .needWashed(false)
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);//-


        //添加评论去重信息

        CrawlerRecord filterCrawlerRecord = new CrawlerRecord();
        filterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange);
        filterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-", filter, domain, site, "comment")));
        filterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(29, null));
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(filterCrawlerRecord));
//        crawlerJob.getScheduleTags().getCategoryTag().addLabelTag(CrawlerEnum.CrawlerDataType.comment.enumVal());


        String resultQueueName = StringUtils.joinWith("-","crawler",site,domain,site_biz,"queue");//-
        CrawlerRequestRecord initRecord = CrawlerRequestRecord.builder()
                .itemPageRequest(crawlerRequestRecord)
                .httpUrl(url)
                .recordKey(url)
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .copyBizTags()
                .build();
        CrawlerRequestRecord initCommentCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain + "_" + site + "_comment", turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/crawler/domain/common/api/v1/" + domain + "/results/queue/init?requestQueueName="
                        + resultQueueName + "&site=" + site + "&hourFromNow=172")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(initRecord), "utf-8");
        initCommentCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initCommentCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);


        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", site, domain,site_biz, CrawlerMetaConstant.ScheduleJobTrigger_Cron))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-", "crawler",site, domain, site_biz, "queue")))//请求队列名
                .kafkaResultPipeline("kafka", kafkaTopic, null)
                .crawlerJobThreadNumber(5)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(initCommentCrawlerRecord)//-
                .build();
        return crawlerJob;
    }

}
